package com.server.llh.llhserverwork.activity.query;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.family.llh.lelaohuitoastview.dialog.toast.T;
import com.google.gson.Gson;
import com.lelaohui.net.listener.NetDataListener;
import com.lelaohui.net.netmanager.INetRequestManager;
import com.lelaohui.net.netmanager.NetRequestManager;
import com.lelaohui.net.request.LRequest;
import com.lelaohui.net.utils.UrlsTools;
import com.llh.llhsuppliers.R;
import com.server.llh.llhserverwork.activity.base.LlhBaseNFCActivity;
import com.server.llh.llhserverwork.activity.food.LlhFoodOrderFragment;
import com.server.llh.llhserverwork.activity.server.LlhQuerySubscribeFragment;
import com.server.llh.llhserverwork.activity.server.LlhServerOrderBaseFragment;
import com.server.llh.llhserverwork.bean.Constants;
import com.server.llh.llhserverwork.bean.UserDataInfoBean;
import com.server.llh.llhserverwork.receiver.GetCardBroadCast;
import com.server.llh.llhserverwork.utils.Constant;
import com.server.llh.llhserverwork.utils.ServerPort;
import com.server.llh.llhserverwork.utils.SharedPreferenceUtil;
import com.server.llh.llhserverwork.utils.ToastTool;
import com.server.llh.llhserverwork.view.CleanableEditText;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by ZTF on 2017-07-14.
 */

public class QueryUserCardInfoActivity extends LlhBaseNFCActivity {
    private static final String TAG = "UserCardInfo";
    @BindView(R.id.titleName)
    TextView titleName;
    @BindView(R.id.main_back)
    ImageButton mainBack;
    @BindView(R.id.leftBtn_RL_title_base)
    RelativeLayout leftBtnRLTitleBase;
    @BindView(R.id.serverName)
    TextView serverName;
    @BindView(R.id.linearLayout1)
    LinearLayout linearLayout1;
    @BindView(R.id.client_card)
    CleanableEditText clientCard;
    @BindView(R.id.client_name)
    CleanableEditText clientName;
    @BindView(R.id.client_money)
    CleanableEditText clientMoney;
    @BindView(R.id.phone)
    CleanableEditText phone;
    @BindView(R.id.client_address)
    CleanableEditText clientAddress;
    @BindView(R.id.commit_my_info)
    Button commitMyInfo;
    private String mToken, userId,intentTag;
 MyBroadFinishActivity myBroadFinishActivity =null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

        titleName.setText("刷卡获取用户信息");
        mToken = SharedPreferenceUtil.getUserInfo(Constants.UserInfo.TOKEN_STR, getApplicationContext());
        IntentFilter filter = new IntentFilter();
        filter.addAction(Constant.READ_CARDACTION);
        getCardBroadCast = new GetCardBroadCast();
        getCardBroadCast.setmHandler(handler);
        getLbm().registerReceiver(getCardBroadCast, filter);
        if (getIntent()!=null){
            intentTag=getIntent().getStringExtra("intentTag");
        }
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Constants.BroadCastAction.FINISH_ALL_ACTIVITY);
        myBroadFinishActivity = new  MyBroadFinishActivity();
        this.registerReceiver(myBroadFinishActivity, intentFilter);
    }

    GetCardBroadCast getCardBroadCast = null;


    String cardId;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == -1000) {
                cardId = String.valueOf(msg.obj);
                Log.d(TAG, "cardId==" + cardId);
                clientCard.setText("" + cardId);
                getUserInfo(String.valueOf(cardId));
            }
        }
    };

    public void getUserInfo(String icNumber) {
        LRequest lRequest = new LRequest();
        lRequest.addParam("token", mToken);
        lRequest.addParam("icNumber", icNumber);
        lRequest.setUri(ServerPort.GET_CARD_INFO);
        lRequest.setUriType("");
        lRequest.setIp(UrlsTools.REQ_URL);
        lRequest.setType(String.class);
        requestData(lRequest, new NetDataListener<String>() {
            @Override
            public void onError(Throwable t) {
                T.showAnimErrorToast(getApplicationContext(), "请求超时！");
            }

            @Override
            public void onNext(String responseBean, String code) {
                if (responseBean != null && "0".equals(code)) {
                    UserDataInfoBean userDataInfoBean = new Gson().fromJson(responseBean, UserDataInfoBean.class);
                    clientName.setText(userDataInfoBean.getData().getUserName());
                    userId=userDataInfoBean.getData().getUserId();
                    phone.setText(userDataInfoBean.getData().getContactPhone());
                    Log.d(TAG,"userId===="+userId);
                    SharedPreferenceUtil.saveUserInfo("customerId",userId,getApplicationContext());
                    Intent intent=null;
                    if ("0".equals(intentTag)) {
                         intent = new Intent(getApplicationContext(), LlhQuerySubscribeFragment.class);
                   }else if ("2".equals(intentTag)){
                          intent=new Intent(getApplicationContext(), LlhFoodOrderFragment.class);
                   }else {
                        intent = new Intent(getApplicationContext(), LlhServerOrderBaseFragment.class);
                    }
                    intent.putExtra("userId",userId);
                    startActivity(intent);
                } else {
                    ToastTool.showText(getApplicationContext(), "没有找到该卡所对应的用户信息！");
                }
            }
        });
    }

    @Override
    public void showProgress() {

    }

    @Override
    protected INetRequestManager getRequestManager() {
        return NetRequestManager.getInstance();
    }

    @Override
    public void hideProgress() {

    }

    @Override
    protected int getlayout() {
        return R.layout.get_client_info;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        try {
            if (getCardBroadCast != null) {
                getLbm().unregisterReceiver(getCardBroadCast);
            }
            if (myBroadFinishActivity!=null){
                this.unregisterReceiver(myBroadFinishActivity);
            }

        }catch (Exception e){

        }

    }

    @Override
    protected void initView() {

    }

    @Override
    protected void doBasess() {

    }


    @OnClick({R.id.main_back, R.id.commit_my_info})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.main_back:
                onBackPressed();
                finish();
                break;
            case R.id.commit_my_info:
                if (userId!=null&&!"".equals(userId)){
                    Intent intent=null;
                    if ("0".equals(intentTag)) {
                        intent = new Intent(getApplicationContext(), LlhQuerySubscribeFragment.class);
                    }else if ("2".equals(intentTag)){
                        intent=new Intent(getApplicationContext(), LlhFoodOrderFragment.class);
                    }else {
                        intent = new Intent(getApplicationContext(), LlhServerOrderBaseFragment.class);
                    }
                    intent.putExtra("userId",userId);
                    startActivity(intent);
                }else{
                    T.showAnimToast(getApplicationContext(),"请刷老人卡！");
                }
                break;
        }
    }

    private class MyBroadFinishActivity extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (Constants.BroadCastAction.FINISH_ALL_ACTIVITY.equals(action)) {

                finish();
            }
        }
    }

}
